** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_D15.smcl", replace

// Baseline
use "$project_path/data/4_regdata/regdata", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// With region and decade fixed effects
use "$project_path/data/4_regdata/regdata", clear
gen decade = Year - mod(Year, 10)
tab decade, gen(d_decade_)
tab Region, gen(d_region_)
rdrobust diff_m1_general_chix_nodem runvar, covs(d_decade_* d_region_*) all

// Trimming instead of winsorizing
use "$project_path/data/4_regdata/robustness/regdata_trim", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// Winsorizing at the 1st/99th percentiles
use "$project_path/data/4_regdata/robustness/regdata_winsor_1_99", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// Winsorizing at the 5th/95th percentiles
use "$project_path/data/4_regdata/robustness/regdata_winsor_5_95", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// 3 post election years
use "$project_path/data/4_regdata/robustness/regdata_3_post_years", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// 5 post election years
use "$project_path/data/4_regdata/robustness/regdata_5_post_years", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// 7 post election years
use "$project_path/data/4_regdata/robustness/regdata_7_post_years", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// 10 post election years
use "$project_path/data/4_regdata/robustness/regdata_10_post_years", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// With year 0 in the post-election period
use "$project_path/data/4_regdata/robustness/regdata_with_year_0", clear
rdrobust diff_m1_general_chix_nodem runvar, all

// Twice the MSE-optimal bandwidth
use "$project_path/data/4_regdata/regdata", clear
rdbwselect diff_m1_general_chix_nodem runvar
local twice_bw = e(h_mserd)*2
rdrobust diff_m1_general_chix_nodem runvar, h(`twice_bw') all

// Half the MSE-optimal bandwidth
use "$project_path/data/4_regdata/regdata", clear
rdbwselect diff_m1_general_chix_nodem runvar
local half_bw = e(h_mserd)/2
rdrobust diff_m1_general_chix_nodem runvar, h(`half_bw') all

// Second-order polynomial
use "$project_path/data/4_regdata/regdata", clear
rdrobust diff_m1_general_chix_nodem runvar, p(2) all

// Uniform kernel
use "$project_path/data/4_regdata/regdata", clear
rdrobust diff_m1_general_chix_nodem runvar, kernel(uni) all

// Epanechnikov kernel
use "$project_path/data/4_regdata/regdata", clear
rdrobust diff_m1_general_chix_nodem runvar, kernel(epa) all

log close



** Building .tex table

//Prepare matrix of coefficients and other statistics
local nrows 15
local ncols 3
mat Coefs = J(`nrows', `ncols', .)

//Initialize row index
local i = 1
//Regressions
local outcome diff_m1_general_chix_nodem

foreach Y in `outcome'{
	
	
	// Baseline
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, all
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// With region and decade fixed effects
	use "$project_path/data/4_regdata/regdata", clear
	gen decade = Year - mod(Year, 10)
	tab decade, gen(d_decade_)
	tab Region, gen(d_region_)
	rdrobust `Y' runvar, covs(d_decade_* d_region_*) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Trimming instead of winsorizing
	use "$project_path/data/4_regdata/robustness/regdata_trim", clear
	rdrobust `Y' runvar, all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Winsorizing at the 1st/99th percentiles and 5th/95th percentiles
	
	foreach win in 1_99 5_95{
		
		use "$project_path/data/4_regdata/robustness/regdata_winsor_`win'", clear
		rdrobust `Y' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''
		
		local ++i
		
	}
	
	//x post election years
	local post_election_year 3 5 7 10 
	foreach year in `post_election_year'{
		
		use "$project_path/data/4_regdata/robustness/regdata_`year'_post_years", clear
		rdrobust `Y' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''
		
		local ++i
	
	}
	
	
	// With year 0 in the post-election period
	use "$project_path/data/4_regdata/robustness/regdata_with_year_0", clear
	rdrobust `Y' runvar, all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Twice the MSE-optimal bandwidth
	use "$project_path/data/4_regdata/regdata", clear
	rdbwselect `Y' runvar
	local twice_bw = e(h_mserd)*2
	rdrobust `Y' runvar, h(`twice_bw') all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Half the MSE-optimal bandwidth
	use "$project_path/data/4_regdata/regdata", clear
	rdbwselect `Y' runvar
	local half_bw = e(h_mserd)/2
	rdrobust `Y' runvar, h(`half_bw') all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Second-order polynomial
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, p(2) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Uniform kernel
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, kernel(uni) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Epanechnikov kernel
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, kernel(epa) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
}

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 est
rename Coefs2 std_err
rename Coefs3 pval

gen outcome = ""

replace outcome = "Baseline" in 1
replace outcome = "With region and decade FE" in 2
replace outcome = "Trimming instead of winsorizing" in 3
replace outcome = "Winsorizing: 1st/99th percentiles" in 4
replace outcome = "Winsorizing: 5th/95th percentiles" in 5
replace outcome = "3 post-election years" in 6
replace outcome = "5 post-election years" in 7
replace outcome = "7 post-election years" in 8
replace outcome = "10 post-election years" in 9
replace outcome = "With year 0 in the post-election period" in 10
replace outcome = "Twice the MSE-optimal bandwidth" in 11
replace outcome = "Half the MSE-optimal bandwidth" in 12
replace outcome = "Second-order polynomial" in 13
replace outcome = "Uniform kernel" in 14
replace outcome = "Epanechnikov kernel" in 15

order outcome, first

tostring est, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace est = est + "***" if pval < `3_star'
replace est = est + "**" if  pval < `2_star' & pval > `3_star'
replace est = est + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}
replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"


label var outcome "Robustness check"
label var pval "p-val."
label var std_err "SE"
label var est "Est."

//Prepare texfile

local title "Robustness checks: General index (without democracy)"
local label_key "tab:robustness_checks_general_chix_nodem"
local output "$project_path/output/appendix_tables/Appendix_Table_D15.tex"
local footnote = "\emph{Notes}: This table reports results for the exercises of Table \ref{tab:robustness_checks_chix_econ} for the general index (excluding democracy)."

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{9.8cm} p{2cm} p{2cm} p{2cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 




